<?php
// Make sure we don't expose any info if called directly
if ( basename( $_SERVER['PHP_SELF'] ) == basename( __FILE__ ) ) {
	die( 'Sorry, but you cannot access this page directly.' );
}

add_action('admin_menu', 'site_basic_page_functions');
function site_basic_page_functions() {
	add_submenu_page('site-basic-setting', '网站性能优化', '网站性能优化', 'manage_options', 'site-basic-function', 'site_basic_function_page' );
}

// Retrieves plugin options if they exist or returns default values if not

function basic_getfunctions() {
	if( get_option( 'basic_functions' )) {
		$basic_functions = get_option( 'basic_functions' );
		if( !isset( $basic_functions['basic_function_login'] ) ){
			$basic_functions['basic_function_login'] = "";
		}
		if( !isset( $basic_functions['basic_function_login_logo'] ) ){
			$basic_functions['basic_function_login_logo'] = "";
		}
		if( !isset( $basic_functions['basic_function_dashboard_clean'] ) ){
			$basic_functions['basic_function_dashboard_clean'] = "";
		}
		if( !isset( $basic_functions['protect_label'] ) ){
			$basic_functions['protect_label'] = "";
		}
		if( !isset( $basic_functions['protect_text'] ) ){
			$basic_functions['protect_text'] = "";
		}
		if( !isset( $basic_functions['basic_function_logo_path'] ) ){
			$basic_functions['basic_function_logo_path'] = "";
		}
	} else {
		$basic_functions['basic_function_login'] = '';
		$basic_functions['basic_function_login_logo'] = '';
		$basic_functions['basic_function_dashboard_clean'] = '';
		$basic_functions['protect_label'] = '';
		$basic_functions['protect_text'] = '';
		$basic_functions['basic_function_logo_path'] = '';
	}
	return $basic_functions;
}

function site_basic_function_page() {
?>
	<div class="wrap">
		<?php settings_errors(); ?>
		<div class="custom_option">
			<form method="post" action="options.php"  class="basic-form-setting">
				<?php
					settings_fields('basic_function_url');
					do_settings_sections( 'basic_functionsection' );
				?>
				<br>
				<button type="submit" name="submit">保存设置</button>
			</form>
		</div>
	</div>
<?php }

add_action( 'admin_init', 'basic_register_functions' );

function basic_register_functions(){

	register_setting( 'basic_function_url', 'basic_functions' );

	add_settings_section( 'basic_function_section', '网站性能优化', 'basic_functiontext', 'basic_functionsection' );
	add_settings_field( 'basic_function_login', '登录保护功能', 'basic_function_login_echo', 'basic_functionsection', 'basic_function_section' );
	add_settings_field( 'basic_function_login_logo', '登录页面logo替换功能', 'basic_function_login_logo_echo', 'basic_functionsection', 'basic_function_section' );
	add_settings_field( 'basic_function_dashboard_clean', '后台显示优化', 'basic_function_dashboard_clean_echo', 'basic_functionsection', 'basic_function_section' );

}

function basic_functiontext(){?>
	<p class="description">优化网站前后端性能</p>
	<script>
		jQuery(document).ready(function($) {
			// 通过jQuery获取checkbox的prop方法获取当前checked的值
			$(".basic-function-checkbox").each(function() {
				$(this).change(function () {
					$(this).val($(this).prop("checked"));
					$(this).next(".upload-section").toggle();
					if ($(".login-protect").prop("checked") === true) {
						$(".login-url").css("text-decoration", "none");
					} else {
						$(".login-url").css("text-decoration", "line-through");
					}
				})
			})
			// 动态查看新登录地址参数
			$(".protect-val").bind("input propertychange", function() {
				$("#" + $(this).data("id")).html($(this).val());
				if ($("#protect_label_val").html() && $("#protect_text_val").html() && $(".login-protect").prop("checked") === true) {
					$(".login-url").css("text-decoration", "none");
				} else {
					$(".login-url").css("text-decoration", "line-through");
				}
			})
		})
	</script>
<?php }

function basic_function_login_echo() {
	$functions = basic_getfunctions();
	$current_function = $functions['basic_function_login'];
	$protect_label = $functions['protect_label'];
	$protect_text = $functions['protect_text'];
?>
	<input class="basic-function-checkbox login-protect" <?php if($current_function) echo 'checked';?> type="checkbox" name="basic_functions[basic_function_login]" value="<?php echo esc_attr($current_function); ?>" />
	启用后，如下两个字段为必填，否则无效，强烈建议不要包含中文<br>
	<input type="text" class="protect-val" data-id="protect_label_val" name="basic_functions[protect_label]" value="<?php echo esc_attr($protect_label); ?>" placeholder="新登录地址参数">
	<input type="text" class="protect-val" data-id="protect_text_val" name="basic_functions[protect_text]" value="<?php echo esc_attr($protect_text); ?>" placeholder="新登录地址结果">
	<div class="login-url" <?php if(!$current_function || !$protect_label || !$protect_text) echo 'style="'.'text-decoration: line-through;'.'"';?>>
		唯一登录地址为<code><?php echo home_url();?>/wp-login.php?<span id="protect_label_val"><?php echo esc_attr($protect_label);?></span>=<span id="protect_text_val"><?php echo esc_attr($protect_text);?></span></code>
	</div>
<?php }

function basic_function_login_logo_echo() {
	$functions = basic_getfunctions();
	$current_function = $functions['basic_function_login_logo'];
	$basic_logo_path = $functions['basic_function_logo_path'];
?>
	<input class="basic-function-checkbox" <?php if($current_function) echo 'checked';?> type="checkbox" name="basic_functions[basic_function_login_logo]" value="<?php echo esc_attr($current_function); ?>" />
	如果启用了，就要上传图片。
	<div class="upload-section" data-id="upload-logo" <?php if(!$current_function) echo 'style="display: none;"';?>>
		<input id="upload-logo" class="image-path" type="text" name="basic_functions[basic_function_logo_path]" value="<?php echo esc_attr($basic_logo_path); ?>">
		<input type="button" value="选择图片" class="upload-image" />
		<hr>
		<img id="upload-logo-image" src="<?php echo $basic_logo_path ? $basic_logo_path : plugins_url('images/demo.png' , __FILE__ );?>" alt="" />
	</div>
<?php }

function basic_function_dashboard_clean_echo() {
	$functions = basic_getfunctions();
	$current_function = $functions['basic_function_dashboard_clean'];
?>
	<input class="basic-function-checkbox" <?php if($current_function) echo 'checked';?> type="checkbox" name="basic_functions[basic_function_dashboard_clean]" value="<?php echo esc_attr($current_function); ?>" />
<?php }

/*替换后台logo*/
function basic_replace_login_head() {
	$functions = basic_getfunctions();
	if ($functions['basic_function_login_logo'] && $functions['basic_function_logo_path']) {
		echo'<style type="text/css"> h1 a {background-image: url('.$functions['basic_function_logo_path'].') !important; } </style>';
	}
}
add_action('login_head', 'basic_replace_login_head');

function basic_loginlogo_url($url) {
	$functions = basic_getfunctions();
	if ($functions['basic_function_login_logo'] && $functions['basic_function_logo_path']) {
		return home_url();
	}
}
add_filter('login_headerurl', 'basic_loginlogo_url');

function basic_loginlogo_title($url) {
	$functions = basic_getfunctions();
	if ($functions['basic_function_login_logo'] && $functions['basic_function_logo_path']) {
		return get_bloginfo( 'description' );
	}
}
add_filter('login_headertitle', 'basic_loginlogo_title');

function site_basic_function_dashboard_clean() {
	$functions = basic_getfunctions();
	if ($functions['basic_function_dashboard_clean']) {
		global $wp_admin_bar;
		$wp_admin_bar->remove_menu('wp-logo'); // 禁用后台左上角的默认icon
		remove_action('welcome_panel', 'wp_welcome_panel'); //禁用仪表盘页面的welcome
	}
}
add_action('wp_before_admin_bar_render', 'site_basic_function_dashboard_clean', 0);

/*移除后台的帮助选项*/
function basic_dashboard_remove_help($old_help, $screen_id, $screen){
	$functions = basic_getfunctions();
	if ($functions['basic_function_dashboard_clean']) {
		$screen->remove_help_tabs();
    	return $old_help;
	}
}
add_filter( 'contextual_help', 'basic_dashboard_remove_help', 999, 3 );

//保护后台登录
function basic_login_protection(){
	$functions = basic_getfunctions();
	$protect_label = $functions['protect_label'];
	$protect_text = $functions['protect_text'];
	if ($functions['basic_function_login'] && $protect_label && $protect_text) {
		if($_GET[$protect_label] != $protect_text)header('Location: '.home_url());
	}
}
add_action('login_enqueue_scripts','basic_login_protection');

